﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.Reporting.WebForms;
using Catering.View_Operaction;
using Catering_Model;
using System.Text;

namespace Catering.Catering_Page.ShanShiFenXi
{
    public partial class PersonAvgContrastLineChar : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void lbtnTyleChar_Click(object sender, EventArgs e)
        {
            string[] lastYears = UCSearchToolByZW1.GetYears();
            string year = "";
            string classid = DDLTool1.GetDDL_Class();
            string areaid = DDLTool1.GetDDL_Area();
            int[] RawId = UCSearchToolByZW1.GetRaw();
            if (lastYears == null || lastYears.Count() < 0 || RawId == null || RawId.Count() <= 0)
            {
                return;
            }
            else
            {
                year = lastYears[0];
            }
            string Title = Bind_Data_to_Controls.Bind_ReportTitle(areaid, classid, DateTime.Parse(year + "-01-01 00:00:11"), "人均消耗统计");
            View_Operaction.View_Monitor vmRules = new View_Operaction.View_Monitor();
            string pid = "0";
            if (Session["UserID"] != null) pid = Session["UserID"].ToString();
            DataTable dt = vmRules.GetPersonAvgContrastForLineChar(pid,year,areaid, classid, RawId);
            BiaoList bl = new BiaoList();

            #region 数据

            if (lastYears.Length > 1)
            {
                bl.biaolist = new Biao[lastYears.Length * RawId.Length];
                int num = 0;
                for (int i = 0; i < RawId.Length; i++)
                {
                    //禽肉类：3、鱼虾类：4、蔬菜类：6、水果类：7、谷类薯类及杂豆：8、油：10、盐：11
                    string rawName = GetRawName(RawId[i]);
                    for (int j = 0; j < lastYears.Length; j++)
                    {
                        bl.biaolist[num] = new Biao();
                        bl.biaolist[num].name = lastYears[j] + "年" + rawName;
                        bl.biaolist[num].data = new decimal[12];
                        num++;
                    }
                }
            }
            else
            {
                bl.biaolist = new Biao[RawId.Length];
                for (int i = 0; i < RawId.Length; i++)
                {
                    //禽肉类：3、鱼虾类：4、蔬菜类：6、水果类：7、谷类薯类及杂豆：8、油：10、盐：11
                    string rawName = GetRawName(RawId[i]);
                    bl.biaolist[i] = new Biao();
                    bl.biaolist[i].name = rawName;
                    bl.biaolist[i].data = new decimal[12];
                }
            }

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string foodtype = dt.Rows[i]["TypeName"].ToString();
                int month = Convert.ToInt32(dt.Rows[i]["Month"]);
                int tempyear = Convert.ToInt32(dt.Rows[i]["NumYear"]);
                decimal number = Convert.ToDecimal(dt.Rows[i]["Number"]);
                for (int j = 0; j < bl.biaolist.Length; j++)
                {
                    if (lastYears.Length == 1)
                    {
                        if (foodtype != bl.biaolist[j].name) continue;
                    }
                    else
                    {
                        if ((tempyear + "年" + foodtype) != bl.biaolist[j].name) continue;

                    }
                    bl.biaolist[j].data[(month - 1)] = number;
                }
            }

            string str = "";
            if (bl != null)
            {
                StringBuilder Sresult = new StringBuilder("");
                foreach (Biao biao in bl.biaolist)
                {
                    Sresult.Append("{");
                    Sresult.Append("name:'" + biao.name + "',");
                    Sresult.Append("data: [");
                    string StrData = "";
                    foreach (decimal num in biao.data)
                    {
                        StrData += num + ",";
                    }
                    StrData = StrData.Length > 0 ? StrData.Substring(0, StrData.Length - 1) : "";
                    Sresult.Append(StrData + "]");
                    Sresult.Append("},");
                }
                str = Sresult.ToString();
            }
            str = str.Length > 0 ? str.Substring(0, str.Length - 1) : "";

            #endregion

            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myScript", "GetDataGroup('" + Title + "',2," + str + ");", true);
            //ReportDataSource rds = new ReportDataSource("DataSet1", dt);
            //ReportViewer1.LocalReport.DataSources.Clear();
            //ReportViewer1.LocalReport.DataSources.Add(rds);
            //ReportViewer1.LocalReport.Refresh();
            ////if (!string.IsNullOrEmpty(classid) && !classid.Equals("-10"))
            ////{
            ////    Catering_Rules.ClassInfo classRules = new Catering_Rules.ClassInfo();
            ////    lblTypeTitle.Text = classRules.GetModel(Int32.Parse(classid)).CName;
            ////}
            //DateTime DTIME = DateTime.Parse("1949-10-01");
            //DateTime.TryParse(year+"-01-01 00:00:11", out DTIME);
            //lblTypeTitle.Text = Bind_Data_to_Controls.Bind_ReportTitle(areaid, classid, DTIME, "全年人均消耗动态图");
            //ReportViewer1.Visible = true;
        }

        public string GetRawName(int type)
        {
            string result = "";
            switch (type)
            {
                case 3:
                    result = "肉类";
                    break;
                case 4:
                    result = "鱼虾类";
                    break;
                case 6:
                    result = "蔬菜类";
                    break;
                case 7:
                    result = "水果类";
                    break;
                case 8:
                    result = "谷类薯类及杂豆";
                    break;
                case 10:
                    result = "油";
                    break;
                case 11:
                    result = "盐";
                    break;
            }
            return result;
        }
    }
}